package com.jamin.custom_view_topbar_path;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.MarginLayoutParams;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.AnticipateInterpolator;
import android.view.animation.OvershootInterpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageButton;

public class MyAnimations{

	private static final String TAG = "MyAnimations=========wangjieming";
	private static int	xOffset		= 15;
	private static int	yOffset		= 13;


	public static void initOffset(Context context){
		xOffset		= (int) (10.667 *context.getResources().getDisplayMetrics().density);
		yOffset		= -(int) (8.667 *context.getResources().getDisplayMetrics().density);
		Log.d( TAG , "xOffset = " + xOffset );
		Log.d( TAG , "yOffset = " + yOffset );
	}

	//旋转动画效果
	public static Animation getRotateAnimation(float fromDegrees ,float toDegrees,int durationMillis){
		RotateAnimation rotate = new RotateAnimation(fromDegrees, toDegrees,
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
		//动画持续时间
		rotate.setDuration(durationMillis);  
		//动画效果结束后是否回归原位
		rotate.setFillAfter(true);
		return rotate;
	}



	//动画飞入
	public static void startAnimationsIn(ViewGroup viewgroup,int durationMillis) {
		//循环ViewGroup中的所有控件
		for (int i = 0; i < viewgroup.getChildCount(); i++) {
			//得到每一个飞入飞出的按钮
			ImageButton inoutimagebutton = (ImageButton) viewgroup.getChildAt(i);
			//设置按钮可见
			inoutimagebutton.setVisibility(View.VISIBLE);

			MarginLayoutParams mlp = (MarginLayoutParams) inoutimagebutton.getLayoutParams();
			//设置移动动画
			Animation animation = new TranslateAnimation(mlp.rightMargin-xOffset,0F, - yOffset - mlp.topMargin   , 0F);
			animation.setFillAfter(true);
			//动画持续时间
			animation.setDuration(durationMillis);
			animation.setStartOffset((i * 100) / (-1 + viewgroup.getChildCount()));
			//设置特殊动画
			animation.setInterpolator(new OvershootInterpolator(2F));
			//启动动画
			inoutimagebutton.startAnimation(animation);

		}
	}


	//动画飞出
	public static void startAnimationsOut(ViewGroup viewgroup,int durationMillis) {
		for (int i = 0; i < viewgroup.getChildCount(); i++) {
			final ImageButton inoutimagebutton = (ImageButton) viewgroup
					.getChildAt(i);
			MarginLayoutParams mlp = (MarginLayoutParams) inoutimagebutton.getLayoutParams();
			Animation animation = new TranslateAnimation(0F,mlp.rightMargin - xOffset , 0F,  - yOffset - mlp.topMargin );

			animation.setFillAfter(true);
			//动画持续时间
			animation.setDuration(durationMillis);
			animation.setStartOffset(((viewgroup.getChildCount()-i) * 100)/ (-1 + viewgroup.getChildCount()));
			animation.setInterpolator(new AnticipateInterpolator(2F));
			animation.setAnimationListener(new Animation.AnimationListener() {
				@Override
				public void onAnimationStart(Animation arg0) {

				}
				@Override
				public void onAnimationRepeat(Animation arg0) {

				}
				@Override
				public void onAnimationEnd(Animation arg0) {
					inoutimagebutton.setVisibility(View.GONE);
				}
			});
			inoutimagebutton.startAnimation(animation);
		}

	}

	//放大动画
	public static void startScaleAnimation(View view ,int durationMillis){
		
		ScaleAnimation animation = new ScaleAnimation(1F, 2F, 1F, 2F, ScaleAnimation.RELATIVE_TO_SELF, 0.5f, ScaleAnimation.RELATIVE_TO_SELF, 0.5f);
		animation.setDuration(durationMillis);
		animation.setInterpolator(new AccelerateDecelerateInterpolator());
		animation.setRepeatCount(1);
		animation.setRepeatMode(ScaleAnimation.REVERSE);
		view.startAnimation(animation);


	}


}